package com.adcc.soundalarm.fx.app;

import com.adcc.soundalarm.common.Constant;
import com.adcc.soundalarm.common.Log;
import com.adcc.soundalarm.config.system.AlarmFactory;
import com.adcc.soundalarm.config.system.AppConfiguration;
import com.adcc.soundalarm.service.RequestService;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by ZHANG on 2017/3/23.
 */
public class SysAlarmViewController {

    // 当前舞台
    private Stage stage;

    // 远程请求类
    private RequestService requestService = RequestService.getInstance();

    AlarmFactory factory = AppConfiguration.getInstance().getAlarmFactory();

    @FXML
    private TextField textHost;

    @FXML
    private TextField textPort;

    @FXML
    private TextField textMuliAdress;

    @FXML
    private TextField textUserUri;

    @FXML
    private TextField textWavPath;

    @FXML
    private TextField textIntervalTime;

    @FXML
    private RadioButton radioMulticast;

    @FXML
    private RadioButton radioUnicast;

    @FXML
    private RadioButton radioBroadcast;


    public void buildEnvironment(Stage stage) {
        this.stage = stage;
    }

    @FXML
    public void handleCancelAction(ActionEvent actionEvent) {
        stage.close();
    }

    private final ToggleGroup group = new ToggleGroup();

    @FXML
    public void handleSaveConfigAction(ActionEvent actionEvent) {
        boolean verifyFlag = true;
        if ("".equals(textIntervalTime.getText().trim())
                || "".equals(textHost.getText().trim())
                || "".equals(textPort.getText().trim())
                || "".equals(textUserUri.getText().trim())
                || "".equals(textWavPath.getText().trim())) {
            verifyFlag = false;
        }
        boolean muliVerifyFlag = false;
        //是否启用组播
        if (radioMulticast.isSelected()) {
            if ( !"".equals(textMuliAdress.getText().trim())) {
                muliVerifyFlag = true;
            }
        } else {
            muliVerifyFlag = true;
        }
        if (verifyFlag && muliVerifyFlag) {
            try {
                saveSysAlarm();
            } catch (Exception ex) {
                Log.error("saveSysAlarm error" + ex);
                Alert alert = new Alert(Alert.AlertType.NONE, "请检查信息格式是否正确！", ButtonType.OK);
                alert.show();
            }
        } else {
            Alert alert = new Alert(Alert.AlertType.NONE, "请检查信息是否完善！", ButtonType.OK);
            alert.show();
        }
    }

    @FXML
    public void handleReadFileAction(ActionEvent actionEvent) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        File file = fileChooser.showOpenDialog(stage);
        if (file != null) {
            textWavPath.setText(file.getPath());
        }
    }



    public void init() {
        //添加到组
        radioMulticast.setToggleGroup(group);
        radioMulticast.setUserData(Constant.YML_TRANSPORT_MULTICAST);
        radioUnicast.setToggleGroup(group);
        radioUnicast.setUserData(Constant.YML_TRANSPORT_UNICAST);
        radioBroadcast.setToggleGroup(group);
        radioBroadcast.setUserData(Constant.YML_TRANSPORT_BROADCAST);
        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                if("1".equals(group.getSelectedToggle()
                        .getUserData())){//组播
                    textMuliAdress.setDisable(false);
                }else {
                    textMuliAdress.setDisable(true);
                }
            }
        });
        //初始化按钮组是否选中
        if (factory != null) {
            switch (factory.getTransport()){
                case 1:{
                    radioMulticast.setSelected(true);
                    break;
                }
                case 2:{
                    radioBroadcast.setSelected(true);
                    break;
                }
                default:{
                    radioUnicast.setSelected(true);
                }
            }
            if (factory.getHost() != null && !"".equals(factory.getHost())) {
                textHost.setText(factory.getHost());
            }
            if (factory.getMultiAddress() != null && !"".equals(factory.getMultiAddress())) {
                textMuliAdress.setText(factory.getMultiAddress());
            }
            if (factory.getPort() > 0) {
                textPort.setText(String.valueOf(factory.getPort()));
            }
            if (factory.getUserUri() != null && !"".equals(factory.getUserUri())) {
                textUserUri.setText(factory.getUserUri());
            }
            if (factory.getIntervalTime() > 0) {
                textIntervalTime.setText(String.valueOf(factory.getIntervalTime()));
            }
            if (factory.getPath() != null && !"".equals(factory.getPath())) {
                textWavPath.setText(factory.getPath());
            }
        }
    }

    public void saveSysAlarm() throws Exception {
        if (textHost.getText() != null && !"".equals(textHost.getText().trim())) {
            factory.setHost(textHost.getText());
        }
        if (textPort.getText() != null && !"".equals(textPort.getText().trim())) {
            factory.setPort(Integer.valueOf(textPort.getText()));
        }
        if (textUserUri.getText() != null && !"".equals(textUserUri.getText().trim())) {
            factory.setUserUri(textUserUri.getText());
        }
        if (textIntervalTime.getText() != null && !"".equals(textIntervalTime.getText().trim())) {
            factory.setIntervalTime(Integer.valueOf(textIntervalTime.getText()));
        }
        if (textWavPath.getText() != null && !"".equals(textWavPath.getText().trim())) {
            factory.setPath(textWavPath.getText());
        }
        if (radioMulticast.isSelected()) { //组播
            factory.setTransport(1);
            factory.setMultiAddress(textMuliAdress.getText().trim());
        } else if (radioBroadcast.isSelected()) { //广播
            factory.setTransport(2);
        }else { //单播
            factory.setTransport(0);
        }
        if (AppConfiguration.getInstance().saveYamlConfig(factory)) {
            Alert alert = new Alert(Alert.AlertType.NONE, "保存成功！", ButtonType.OK);
            alert.show();
            //重启udp
            requestService.start();
        } else {
            Alert alert = new Alert(Alert.AlertType.NONE, "保存失败！", ButtonType.OK);
            alert.show();
        }
    }
}
